package com.atguigu.tx.service.impl;

import com.atguigu.tx.dao.BookDao;
import com.atguigu.tx.exception.BookStockException;
import com.atguigu.tx.exception.UserBalanceException;
import com.atguigu.tx.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Date:2020/9/25
 * Author:ybc
 * Description:
 */
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao bookDao;

    @Override
    //@Transactional(noRollbackFor = {UserBalanceException.class})
    //@Transactional(timeout = 3)
    public void buyBook(Integer userId, Integer bookId) {

        /*try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/

        Integer price = bookDao.getPriceByBookId(bookId);
        bookDao.updateBookStock(bookId);
        bookDao.updateUserBalance(userId, price);
    }
}
